package com.djinfo.work.dao.impl;
import com.djinfo.work.common.dao.impl.BaseDaoImpl;
import com.djinfo.work.dao.UserDao;
import com.djinfo.work.entity.TbSysMenu;
import com.djinfo.work.entity.TbSysRoles;
import com.djinfo.work.entity.TbSysUsers;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 实现 用户登录 首页导航显示
 *
 * @author ming dao的实现类
 */
@Repository("UserDao")
public class UserDaoImpl extends BaseDaoImpl implements UserDao {
	public void save(TbSysUsers tbSysUsers) {
		if (tbSysUsers.getPkUserId() == null) {
			super.save(tbSysUsers);
		} else {
			if(StringUtils.isEmpty(tbSysUsers.getUserPassword())){

				TbSysUsers tbSysUsers1 = this.get(tbSysUsers.getPkUserId());
				tbSysUsers.setUserPassword(tbSysUsers1.getUserPassword());
			}
			super.merge(tbSysUsers);
		}
	}

	@Override
	public void delete(String id) {
		TbSysUsers tbSysUsers = this.get(id);
		super.delete(tbSysUsers);
	}

	public List<TbSysUsers> getAllUser() {
		return (List<TbSysUsers>) pageQuery("from TbSysUsers", null, null);
	}

	/**
	 * 用户登录功能
	 *
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return 登录成功返回用户ID ，失败返回0
	 */
	@Override
	public String getUserID(String username, String password) {
		// TODO Auto-generated method stub
		String hql="select pkUserId from TbSysUsers where  userName=? and userPassword=?";
		// 给hql参数赋值

		List<?> lResult =  super.getList(hql,username,password);
		if (!(lResult.isEmpty() || lResult.size() == 0)) {
			String isnameorpass = (String)  super.getUniqueObject(hql,username,password);
			return isnameorpass;
		} else {
			return "";
		}

	}

	/**
	 * 用户登录功能
	 *
	 *            用户名
	 * @return 登录成功返回用户ID ，失败返回0
	 */
	@Override
	public Integer getUserID(String userCode) {
		// TODO Auto-generated method stub
		String hql="select pkUserId from TbSysUsers where  userCode=?";
		// 给hql参数赋值

		List<?> lResult =  super.getList(hql,userCode);
		if (!(lResult.isEmpty() || lResult.size() == 0)) {
			Integer isnameorpass = (Integer)  super.getUniqueObject(hql,userCode);
			return isnameorpass;
		} else {
			return 0;
		}

	}

	@Override
	public TbSysUsers getUserByUserCode(String userCode) {
		String hql="from TbSysUsers where userCode=?";
		List<?> lResult =  super.getList(hql,userCode);
		if (!(lResult.isEmpty() || lResult.size() == 0)) {
			return (TbSysUsers) super.getUniqueObject(hql, userCode);
		} else {
			return null;
		}

	}

	@Override
	public List<TbSysRoles> getUserRoles(String UserId) {
		String hql = "from TbSysRoles b  where EXISTS (select a.roleId from TbSysRoleUser a where b.pkRoleId = a.roleId and a.userId = ? )";
		return super.getList(hql,""+UserId);
	}

	/**
	 * 左侧导航显示 根据id查询导航菜单信息
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<TbSysMenu> authoritiesfindAll(Integer id) {
		// TODO Auto-generated method stub
		String hql=" from TbSysMenu where parentAuthId=?";
		return super.getList(hql,id);
	}


	/**
	 * 根据主键id查询用户信息 可以返回null
	 */
	@Override
	public TbSysUsers get(String id) {
		// TODO Auto-generated method stub
		return (TbSysUsers) this.sessionFactory.getCurrentSession().get(TbSysUsers.class, id);
	}

	@Override
	public List<TbSysUsers> page(TbSysUsers tbSysUsers, Integer pageNum, Integer pageSize) {
		String hql="";
		if(tbSysUsers.getUserName()==null||"".equals(tbSysUsers.getUserName())){
			hql = " from TbSysUsers where 1=1 ";
		}else {
			hql = " from TbSysUsers where userName like '%"+ tbSysUsers.getUserName()+ "%'";
		}

		return (List<TbSysUsers>)super.pageQuery( hql,  pageNum,  pageSize);
	}

	@Override
	public Integer getAllRowCount(TbSysUsers tbSysUsers) {
		String hql="";
		if(tbSysUsers.getUserName()==null||"".equals(tbSysUsers.getUserName())){
			hql="select count(*) from TbSysUsers ";
		}else {
			hql = "select count(*) from TbSysUsers where userName like '%"+ tbSysUsers.getUserName()+ "%'";
		}

		return super.getUniqueObject(hql).hashCode();
	}

	@Override
	public void update(TbSysUsers tbSysUsers) {
		super.update(tbSysUsers);
	}

	@Override
	public ArrayList<TbSysUsers> getUsers(String deptId) {
		String hql="from TbSysUsers where deptId='"+deptId+"'";
		return (ArrayList<TbSysUsers>)super.getList(hql);
	}

	@Override
	public List getList(String hql, Object... objects) {
		return super.getList(hql,objects);
	}

	@Override
	public void deleteQsList() {
		String hql = " delete FROM  QsUserQuestionEntity where qid is "+null+" ";
		super.updateByHQL(hql);
	}


}

